package com.wonders.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonders.dto.Result;
import com.wonders.dto.UserDTO;
import com.wonders.entity.Blog;
import com.wonders.service.IBlogService;
import com.wonders.utils.SystemConstants;
import com.wonders.utils.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述:探店笔记表相关业务
 * @Author: yyalin
 * @CreateDate: 2023/8/14 13:17
 */
@Api(tags="探店笔记表相关业务")
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource
    private IBlogService blogService;
    @ApiOperation(value="保存探店笔记信息", notes="saveBlog")
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        return blogService.saveBlog(blog);
    }

//    @ApiOperation(value="点赞功能", notes="likeBlog")
//    @PutMapping("/like/{id}")
//    public Result likeBlog(@PathVariable("id") Long id) {
//        return blogService.likeBlog(id);
//    }
    @ApiOperation(value="点赞/取消功能", notes="likeBlog")
    @PostMapping("/liked")
    public Result likeBlog(@RequestParam("id") Long id,@RequestParam("userId") Long userId) {
        return blogService.likeBlog(id,userId);
    }

    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHotBlog(current);
    }

    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id) {
        return blogService.queryBlogById(id);
    }

    @ApiOperation(value="根据ID查询点赞的所有用户", notes="queryBlogLikes")
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id) {
        return blogService.queryBlogLikes(id);
    }

    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }
    @ApiOperation(value="查询关注的所有探店笔记(并含滚动分页)", notes="queryBlogOfFollow")
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(
            @RequestParam("lastId") Long max,
            @RequestParam(value = "offset", defaultValue = "0") Integer offset){
        return blogService.queryBlogOfFollow(max, offset);
    }
}
